package com.taiji.dianthus.util;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;


/**
 * 导入excel
 */
public class ExcelUtil {

    /**
     * 获取sheet
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static Sheet getDataFromExcel(MultipartFile file) throws IOException {
        InputStream fis = null;
        Workbook wookbook = null;
        Sheet sheet = null;
        try {
            //2003版本的excel，用.xls结尾
            wookbook = new HSSFWorkbook(file.getInputStream());//得到工作簿
        } catch (Exception ex) {
            //ex.printStackTrace();
            try {
                wookbook = new XSSFWorkbook(file.getInputStream());//得到工作簿提供了对2007版本以及更高版本的支持
                sheet = getAccuracyContextNum(wookbook);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                //使用完成关闭
                wookbook.close();
                if (fis != null) {
                    fis.close();
                }
            }
        }
        return sheet;
    }


    //获取准确的文件行数
    public static Sheet getAccuracyContextNum(Workbook workbook) {
        // 取第一个sheet
        Sheet sheet = workbook.getSheetAt(0);
        // 删除空行
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            // 删除空行
            if (isRowEmpty(row)) {
                int lastRowNum = sheet.getLastRowNum();
                if (i >= 0 && i < lastRowNum) {
                    sheet.shiftRows(i + 1, lastRowNum, -1);// 将行号为i+1一直到行号为lastRowNum的单元格全部上移一行，以便删除i行
                }
                if (i == lastRowNum) {
                    if (row != null) {
                        sheet.removeRow(row);
                    }
                }
                i--;
            }
        }
        return sheet;
    }

    //判断row是否为空
    public static boolean isRowEmpty(Row row) {
        for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c);
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
                return false;
            }
        }
        return true;
    }

    //把每一个cell转换为string
    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell == null) {
            return cellValue;
        }
        //把数字转换成string，防止12.0这种情况
        if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
            cell.setCellType(cell.CELL_TYPE_STRING);
        }
        //判断数据的类型
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC: //数字0
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING: //字符串1
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN: //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                //cellValue = String.valueOf(cell.getCellFormula());
                try {
                    cellValue = String.valueOf(cell.getNumericCellValue());
                } catch (IllegalStateException e) {
                    cellValue = String.valueOf(cell.getRichStringCellValue());
                }
                break;
            case Cell.CELL_TYPE_BLANK: //空值
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR: //故障
                cellValue = "非法字符";
                break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue;
    }

    //检查文件类型
    public static Boolean checkFile(MultipartFile file) {
        //检查文件是否为空
        if (file == null || file.isEmpty()) {
            return false;
        }
        //检查文件是否是excel类型文件
        String filename = file.getOriginalFilename();
        if (!filename.endsWith("xls") && !filename.endsWith("xlsx")) {
            return false;
        }
        return true;
    }

}
